﻿namespace SS.WPF.Framework.Restriction
{
    using System.Collections.Generic;
    using System.Linq;

    public class CompositeRestriction : IRestriction
    {
        public IList<IRestriction> Restrictions { get; set; }

        public CompositeRestriction()
        {
            Restrictions = new List<IRestriction>();
        }

        #region Implementation of IRestriction

        public bool Validate(object value)
        {
            return !IsEnabled || Restrictions.All(restriction => restriction.Validate(value));
        }

        public string Description
        {
            get
            {
                return this.Restrictions.Count > 0 ? string.Join(" ", this.Restrictions.Select(x => x.Description).ToArray()) : null;
            }
        }

        public bool CanParse(string text)
        {
            return Restrictions.All(restriction => restriction.CanParse(text));
        }

        private bool _isEnabled = true;

        public bool IsEnabled
        {
            get
            {
                return this._isEnabled;
            }
            set
            {
                this._isEnabled = value;
            }
        }

        #endregion
    }
}